#pragma once

#include <vector>
#include <winsock2.h>


class SSession;
class ConnectBase
{
public:
	ConnectBase(SSession* pSession) : m_pSession(pSession)
	{
		m_socket = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED ); 		
	}
	virtual ~ConnectBase()
	{
		m_pSession = nullptr;
	}

	bool Connect(std::string ip, int port);

	virtual void OnConnect(SOCKET s, bool result);


	SOCKET GetSocket() {return m_socket;}
	SOCKET m_socket;
	SSession* m_pSession;
	struct sockaddr_in m_sin;
};

template<typename T>
class ConnectT : public ConnectBase
{
public:
	ConnectT(){}
	virtual ~ConnectT(){}

	virtual void OnConnect(SOCKET s, bool result) {T::OnConnect(s, result); }
};

class ConnectManager
{
public:
	ConnectManager():m_bRun(true){}
	~ConnectManager()
	{
		for each( HANDLE handle in m_handleVector)
			::CloseHandle(handle);

		for each( ConnectBase* pConnector in m_connectorVector)		
			delete pConnector;	
	}

	void Register(ConnectBase* pConnectBase);
	bool ThreadConnect();

	bool m_bRun;
private:
	std::vector<HANDLE>		m_handleVector;
	std::vector<ConnectBase*>	m_connectorVector;
};
